<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("menus.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Vehicle_Misc_Edit extends Page
{
       public $hfForm = null;
       public $edRef = null;
       public $Label4 = null;
       public $JSRoundDecimal = null;
       public $Label1 = null;
       public $lblPrice = null;
       public $hfType = null;
       public $compid = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $btnShowAccont = null;
       public $JTDivAccount = null;
       public $JTGroupBox4 = null;
       public $btnAddAccount = null;
       public $btnDelAccount = null;
       public $edAccount = null;
       public $listAccount = null;
       public $Label2 = null;
       public $hiddenMisc = null;
       public $btnUpdate = null;
       public $memodesc = null;
       public $Label3 = null;
       public $btnCancel = null;
       public $JTDate = null;
       public $edAmt = null;
       public $cmbAcct = null;
       public $lblAcct = null;
       public $memoNotes = null;
       public $edPrice = null;
       public $edQty = null;
       public $lblNotes = null;
       public $lblQty = null;
       public $Image1 = null;
       public $hiddenWO = null;
       public $hiddenVeh = null;
       public $JTSiteTheme1 = null;

       function btnDelAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var AccountIndex = document.getElementById('listAccount').selectedIndex;
       var AccountValue = document.getElementById('listAccount')[AccountIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxAccount3(AccountValue,comp);
       <?php

       }

       function btnAddAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addaccount = document.getElementById('edAccount').value;
       var compider = document.getElementById('compid').value;
       AjaxAccount2(addaccount,compider);
       document.getElementById('edAccount').value = "";
       <?php

       }

       function btnShowAccontJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivAccount').Show();
       document.getElementBYId('edAccount').value = "";
       <?php

       }

       function btnCancelJSClick($sender, $params)
       {

       ?>
       window.close();
       <?php

       }

       function edPriceJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.Vehicle_Misc_Edit.edAmt.value = roundNumber((document.Vehicle_Misc_Edit.edQty.value) * (document.Vehicle_Misc_Edit.edPrice.value),2);


       //accept only numbers
       var numero = document.getElementById("edPrice").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edPrice").value = numero;

       <?php

       }

       function edQtyJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.Vehicle_Misc_Edit.edAmt.value = roundNumber((document.Vehicle_Misc_Edit.edQty.value) * (document.Vehicle_Misc_Edit.edPrice.value),2);
       <?php

       }

       function btnUpdateClick($sender, $params)
       {

       $query_update = "UPDATE veh_misc_tbl SET
       date='".$this->JTDate->Text."',
       reference_num='".$this->edRef->Text."',
       description='".$this->memodesc->Text."',
       qty='".$this->edQty->Text."',
       price='".$this->edPrice->Text."',
       amount='".$this->edAmt->Text."',
       account='".$this->cmbAcct->getItemIndex()."',
       notes='".$this->memoNotes->Text."'
       WHERE workorder='".$this->hiddenWO->getValue()."' and misc_id ='".$this->hiddenMisc->getValue()."'";

       if($this->hfForm->getValue()=='summary')
       {
       $type = $this->hfType->getValue();
       if($type=='create')
       {
       mysql_query($query_update) or die(mysql_error());

       //redirect('veh_parts_list.php?veh='.$this->hiddenVeh->getValue().'&WO='.$this->hiddenWO->getValue());
       echo '<script language=javascript>
       window.opener.parent.setWODetails();
       window.opener.location.reload();
       alert("Successfully Updated Record!");
       window.close();
       </script>';
       }
       if($type=='edit')
       {
       $filename = "temp/misc_update.txt";
       $handle = fopen($filename, "w");
       fwrite($handle, $query_update);
       fclose($handle);

       echo "<script type=text/javascript>
       var wo = ".$this->hiddenWO->getValue().";
       var veh= ".$this->hiddenVeh->getValue()."
       var misc = ".$this->hiddenMisc->getValue()."
       window.location.href='misc_endorsement.php?WO='+wo+'&veh='+veh+'&misc='+misc;
       </script>
       ";
       }
       }

       if($this->hfForm->getValue()=='order')
       {
       mysql_query($query_update) or die(mysql_error());

       //redirect('veh_parts_list.php?veh='.$this->hiddenVeh->getValue().'&WO='.$this->hiddenWO->getValue());
       echo '<script language=javascript>
       window.opener.parent.setWODetails();
       window.opener.location.reload();
       alert("Successfully Updated Record!");
       window.close();
       </script>';
       }

       }

       function Vehicle_Misc_EditCreate($sender, $params)
       {
       $this->compid->Value = $_SESSION['company_id'];
       $veh_id = $_GET['veh'];
       $veh_misc = $_GET['misc'];
       $WO = $_GET['WO'];

       if($veh_id!=null || $veh_id!='')
       $this->hiddenVeh->setValue($veh_id);
       if($veh_misc!=null || $veh_misc!='')
       $this->hiddenMisc->setValue($veh_misc);
       if($WO!=null || $WO!='')
       $this->hiddenWO->setValue($WO);

       $type = $_GET['type'];
       if($type!=null || $type!='')
       $this->hfType->setValue($type);

       $form = $_GET['form'];
       if($form!=null || $form!='')
       $this->hfForm->setValue($form);

       if($_SESSION['access_vehicle']==1)
       {
       $this->btnCancel->Visible=false;
       $this->btnUpdate->Visible=false;
       }

       if($_SESSION['access_vehicle']==2)
       {
       $this->btnCancel->Visible=true;
       $this->btnUpdate->Visible=true;
       }


       $query1 = "select * from veh_misc_tbl where workorder='".$this->hiddenWO->getValue()."' and misc_id='".$this->hiddenMisc->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());
       $row = mysql_fetch_array($result);

       $this->edRef->Text=$row['reference_num'];
       $this->JTDate->Text=$row['date'];
       $this->edQty->Text = $row['qty'];
       $this->edPrice->Text = $row['price'];
       $this->edAmt->Text = $row['amount'];
       $this->memoNotes->Text = $row['notes'];
       $this->memodesc->Text = $row['description'];

       $this->cmbAcct->Clear();
       $this->cmbAcct->ItemIndex=$row['account'];
        $result_account = mysql_query("select value from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='accounts'");

        while($row_account = mysql_fetch_array($result_account))
        {
            $this->cmbAcct->AddItem($row_account['value'], null, $row_account['value']);
        }

       //list account
       $this->listAccount->Clear();
        $result_ac = mysql_query("select value from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='accounts'");

        while($row_ac = mysql_fetch_array($result_ac))
        {
            $this->listAccount->AddItem($row_ac['value'], null, $row_ac['value']);
        }

       }

       function btnCancelClick($sender, $params)
       {
       redirect('veh_parts_list.php?veh='.$this->hiddenVeh->getValue().'&WO='.$this->hiddenWO->getValue());
       }



}

global $application;

global $Vehicle_Misc_Edit;

//Creates the form
$Vehicle_Misc_Edit=new Vehicle_Misc_Edit($application);

//Read from resource file
$Vehicle_Misc_Edit->loadResource(__FILE__);

//Shows the form
$Vehicle_Misc_Edit->show();

?>